package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BompMchntBindInfoMapper;
import cc.rengu.igas.bomp.common.entity.BspsMchntBindInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户绑定.
 * Updated by 800p_tangfy on 2020/04/02 --新增收银员解绑方法
 *
 * @author sccba  
 * @version 1.0
 * @date 2019/08/15  
 */
public class BompMchntBindInfoMapperImpl implements BompMchntBindInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);


    @Override
    public BspsMchntBindInfo selectBompMchntBindInfoListByUserIdAndMchntNo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBindInfoByUserIdAndMchntNo");
        BspsMchntBindInfo mchntBindInfo = new BspsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindUserId(userId);
        //绑定
        mchntBindInfo.setBindStatus("00");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.error("根据用户Id<{}>，商户号：<{}>获取商户绑定信息为空!", userId, mchntNo);
            return null;
        }
        return mchntBindInfo;
    }

    @Override
    public List<BspsMchntBindInfo> selectBompMchntBindInfoListByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBindInfoListByUserId");
        BspsMchntBindInfo mchntBindInfo = new BspsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindUserId(userId);
        //绑定
        mchntBindInfo.setBindStatus("00");
        List<Object> objectList = dbsUtil.dbsFetchListData(sqlStr, mchntBindInfo, BspsMchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取绑定关系信息失败,instId:<{}>,userId:{}", instId, userId);
            return null;
        }
        return objectList.stream().map(item -> (BspsMchntBindInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<BspsMchntBindInfo> selectBompMchntBindInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompMchntBindInfoByMchntNo");
        BspsMchntBindInfo mchntBindInfo = new BspsMchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchListData(sqlStr, mchntBindInfo, BspsMchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取绑定关系信息失败,instId:<{}>,mchntNo:{}", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (BspsMchntBindInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateBompMchntBindInfoByMchntStopOrCancel(BspsMchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompMchntBindInfoByMchntStopOrCancel");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑收银员失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBompMchntBindInfoByPrimaryKey(BspsMchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompMchntBindInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑收银员失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }
}
